package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.entity.vo.BannerQuery;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-16
 */
@Api(description="后台banner")
@RestController
@RequestMapping("/cmsservice/crmbanner")
//@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService bannerService;


    @ApiOperation(value = "带条件分页查询banner列表")
    @PostMapping("getBannerPageVo/{current}/{limit}")
    public R getBannerPageVo(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody BannerQuery bannerQuery){
        //@RequestBody 把json串转化成实体类
        //1、取出查询条件
        String title = bannerQuery.getTitle();
        Integer isDeleted = bannerQuery.getIsDeleted();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        //2、判断条件是否为空，如不为空拼写sql
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(isDeleted)){
            wrapper.eq("is_deleted", isDeleted);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }


        Page<CrmBanner> page = new Page<>(current,limit);
        bannerService.page(page, wrapper);
        List<CrmBanner> records = page.getRecords();
        long total = page.getTotal();
        //1、存入MAP
//        Map<String,Object> map = new HashMap<>();
//        map.put("list", records);
//        map.put("total", total);
//        return R.ok().data(map);
        //2、直接拼接
        return R.ok().data("list", records).data("total", total);
    }

    @ApiOperation(value = "根据id查询banner")
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        CrmBanner crmBanner = bannerService.getById(id);
        return R.ok().data("crmBanner", crmBanner);
    }

    @ApiOperation(value = "修改banner信息")
    @PostMapping("updateBannerInfo")
    public R updateBannerInfo(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "添加banner图片")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = bannerService.save(crmBanner);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID删除banner图片")
    @DeleteMapping("{id}")
    public R delBanner(@PathVariable String id){
        boolean remove = bannerService.removeById(id);
        if (remove){
            return R.ok();
        }else {
            return R.error();
        }

    }






}

